package com.jackiepenghe.rangedatepickerlibrary

import java.util.Calendar
import java.util.Date

/**
 * 将传入的Date 月份+number后返回
 */
internal fun Date.addMonth(number: Int): Date {
    val calendar = Calendar.getInstance()
    calendar.time = this
    calendar.add(Calendar.MONTH, number)
    return calendar.time
}

internal fun Date.addDay(number: Int): Date {
    val calendar = Calendar.getInstance()
    calendar.time = this
    calendar.add(Calendar.DAY_OF_MONTH, number)
    return calendar.time
}

@Suppress("UnusedReceiverParameter")
internal fun Any.getCurrentDate(): Date {
    val date = Date(System.currentTimeMillis())
    val calendar = Calendar.getInstance()
    calendar.time = date
    calendar.set(Calendar.HOUR_OF_DAY, 0)
    calendar.set(Calendar.MINUTE, 0)
    calendar.set(Calendar.SECOND, 0)
    calendar.set(Calendar.MILLISECOND, 0)
    return calendar.time
}

internal fun Date.keepDateOnly(): Date {
    val calendar = Calendar.getInstance()
    calendar.time = this
    calendar.set(Calendar.HOUR_OF_DAY, 0)
    calendar.set(Calendar.MINUTE, 0)
    calendar.set(Calendar.SECOND, 0)
    calendar.set(Calendar.MILLISECOND, 0)
    return calendar.time
}

/**
 * 将一个日期字符串转为Date类
 */
internal fun String.toDate(date: Date): Date? {
    if (isEmpty()) {
        return null
    }
    val dateInt = try {
        this.toInt()
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    }
    val calendar = Calendar.getInstance()
    calendar.time = date
    calendar.set(Calendar.DAY_OF_MONTH, dateInt)
    return calendar.time
}

internal fun Date.isActive(dataRecyclerViewAdapter: DataRecyclerViewAdapter): Boolean {
    if (dataRecyclerViewAdapter.enableBeforeActiveDate) {
        return true
    }
    return this.after(dataRecyclerViewAdapter.dateActiveDate) || this == dataRecyclerViewAdapter.dateActiveDate
}

internal fun Date.isSelect(dataRecyclerViewAdapter: DataRecyclerViewAdapter): Boolean {
    if (dataRecyclerViewAdapter.startDate == null) {
        return false
    }
    if (this == dataRecyclerViewAdapter.startDate) {
        return true
    }
    if (dataRecyclerViewAdapter.endDate == null) {
        return false
    }
    if (this == dataRecyclerViewAdapter.endDate) {
        return true
    }
    return this.after(dataRecyclerViewAdapter.startDate) && this.before(dataRecyclerViewAdapter.endDate)
}

internal fun Date.isStartDate(dataRecyclerViewAdapter: DataRecyclerViewAdapter): Boolean {
    return this == dataRecyclerViewAdapter.startDate && dataRecyclerViewAdapter.endDate != null
}

internal fun Date.isSingleSelect(dataRecyclerViewAdapter: DataRecyclerViewAdapter): Boolean {
    return this == dataRecyclerViewAdapter.startDate && dataRecyclerViewAdapter.endDate == null
}

internal fun Date.isEndDate(dataRecyclerViewAdapter: DataRecyclerViewAdapter): Boolean {
    return this == dataRecyclerViewAdapter.endDate
}

internal fun Date.differDay(date: Date): Int {
    val differTime = if (this.after(date)) {
        this.time - date.time
    } else {
        date.time - this.time
    }
    val differSecond = differTime / 1000
    val differMinute = differSecond / 60
    val differHour = differMinute / 60
    return (differHour / 24).toInt()
}